/*
 * FileManager.cpp
 *
 *  Created on: Oct 21, 2012
 *      Author: bruno
 */

#include "FileManager.h"

FileManager::FileManager(std::string workingFile, unsigned int n, std::string outputFileName, bool integerToOutput) {

	this->workingFile = workingFile;

	bufferSizeFactor = n;

	size_t pos = workingFile.find_last_of('/');
	if (pos != std::string::npos) {
		workingDir = workingFile.substr(0, pos + 1);
	} else {
		workingDir = ".";
	}
	readingDir = workingDir;

	tempLevel = 1;

	currentNewTempFileNumber = currentTempFileNumber = 1;

	maxTempFileNumber = currentTempFileNumber;

	outputInteger = integerToOutput;

	tempFolderName = std::string("TempFolder");

	printTmpLevel = false;

	this->outputFileName = outputFileName;
}

FileManager::~FileManager() {

}

void FileManager::setWorkingDir(std::string workingDir) {
	this->workingDir = workingDir;
}

std::string FileManager::getWorkingDir() {
	return workingDir;
}

void FileManager::setTempFolderName(std::string tempFolderName) {

	size_t pos = workingFile.find_last_of('/');

	if (pos != std::string::npos) {

		workingDir = workingFile.substr(0, pos + 1);

	} else {

		workingDir = ".";

	}

	this->tempFolderName = tempFolderName;

	maxTempFileNumber = currentNewTempFileNumber;

	currentNewTempFileNumber = 1;
}

std::string FileManager::getTempFolderName() {
	return tempFolderName;
}

IDS_ptr FileManager::getInput() {
	return IDS_ptr (new DictionaryFileImplementation(workingFile.c_str(),
			std::ios_base::in, bufferSizeFactor, bufferSizeFactor,
			true));
}

IDS_ptr FileManager::getOutput() {

//	std::string filePath;
//
//	size_t pos = workingFile.find_last_of('/');
//
//	if (pos != std::string::npos) {
//
//		filePath = workingFile.substr(0, pos + 1);
//
//	} else {
//
//		filePath = ".";
//
//	}
//
//	filePath = filePath + FILESYSTEM_SEPARATOR + outputFileName;

	std::string filePath = outputFileName;

	return IDS_ptr(new DictionaryFileImplementation(filePath.c_str(),
			std::ios_base::out | std::ios_base::trunc, bufferSizeFactor,
			bufferSizeFactor, outputInteger));
}

IDS_ptr FileManager::getNewTempFile() {

	if (currentNewTempFileNumber == 1) {

		if (tempLevel == 1) {
			workingDir = workingDir + FILESYSTEM_SEPARATOR + tempFolderName;

			if (printTmpLevel)
				workingDir = workingDir + intToString(tempLevel);

			std::cout << std::endl << "Temp name:" << workingDir;
		}

		if (mkdir(workingDir.c_str(), 0777) == 0)
			std::cout << std::endl << "Temp creation: OK";
	}

	std::string numberString = intToString(currentNewTempFileNumber);

	std::string filePath = workingDir + std::string(FILESYSTEM_SEPARATOR)
			+ std::string("Temp_") + numberString;

	std::cout << std::endl << "filePathNew:" << filePath << std::endl;

	currentNewTempFileNumber++;

	return IDS_ptr( new DictionaryFileImplementation(filePath.c_str(),
			std::ios_base::out | std::ios_base::trunc, bufferSizeFactor,
			bufferSizeFactor, true) );
}

IDS_ptr FileManager::getNextTempFile() {

	if (currentTempFileNumber == 1 && tempLevel == 1) {
		readingDir = readingDir + FILESYSTEM_SEPARATOR + tempFolderName;
	}

	if (workingDir == readingDir
			&& currentTempFileNumber == currentNewTempFileNumber)
		return IDS_ptr();

	if (workingDir != readingDir && currentTempFileNumber == maxTempFileNumber)
		return IDS_ptr();

	std::string numberString = intToString(currentTempFileNumber);

	std::string filePath = readingDir + std::string(FILESYSTEM_SEPARATOR)
			+ std::string("Temp_") + numberString;
	std::cout << std::endl << "filePathNext:" << filePath << std::endl;

	currentTempFileNumber++;

	return IDS_ptr( new DictionaryFileImplementation(filePath.c_str(), std::ios_base::in,
			bufferSizeFactor, bufferSizeFactor, true));
}

void FileManager::increaseLevel() {
	tempLevel++;

	maxTempFileNumber = currentNewTempFileNumber;

	currentNewTempFileNumber = 1;

	readingDir = workingDir;

	workingDir = workingDir + FILESYSTEM_SEPARATOR + tempFolderName;

	if (printTmpLevel)
		workingDir = workingDir + intToString(tempLevel);

	currentTempFileNumber = 1;
}

void FileManager::setPrintTmpLevel(bool boolean) {
	printTmpLevel = boolean;
}

std::string FileManager::intToString(int number) {
	std::stringstream numberComverter;

	numberComverter << number;

	return numberComverter.str();
}
